package com.chenfan.process.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.chenfan.ccp.common.result.R;
import com.chenfan.ccp.util.tool.JsonUtil;
import com.chenfan.infra.annotation.Log;
import com.chenfan.process.biz.ApprovalTemplateBiz;
import com.chenfan.process.biz.ApprovalTemplateQueryBiz;
import com.chenfan.process.entity.dto.ApprovalTemplateCodeDTO;
import com.chenfan.process.entity.dto.ApprovalTemplateDTO;
import com.chenfan.process.entity.dto.ApprovalTemplateQueryDTO;
import com.chenfan.process.entity.dto.ApprovalTemplateReceivedDTO;
import com.chenfan.process.entity.po.ApprovalTemplatePo;
import com.chenfan.process.entity.vo.ApprovalTemplateDetailVO;
import com.chenfan.process.entity.vo.ApprovalTemplateVo;
import com.chenfan.process.util.NodeUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 模板API
 * 
 * @author liuming
 * @date 2021/10/26
 */
@Api(tags = "模板API")
@RequestMapping(value = "/approval/template")
@RestController
public class ApprovalTemplateController {

    @Autowired
    private ApprovalTemplateBiz approvalTemplateBiz;

    @Autowired
    private ApprovalTemplateQueryBiz approvalTemplateQueryBiz;

    @ApiOperation("获取模板信息")
    @GetMapping("")
    public R<List<ApprovalTemplateVo>> listTemplate() {
        return R.ok(approvalTemplateQueryBiz.listTemplate());
    }

    /**
     * 校验模板编码和名称是否存在
     */
    @ApiOperation(value = "校验模板编码和名称是否存在")
    @PostMapping(value = "/check")
    public R<Void> preCheck(@RequestBody ApprovalTemplateCodeDTO dto) {
        approvalTemplateBiz.preCheck(dto);
        return R.ok();
    }

    /**
     * 创建审批模板
     *
     * @param template
     *            模板
     * @return {@link R}<{@link Long}>
     */
    @Log(title = "创建审批模板")
    @ApiOperation(value = "创建审批模板")
    @PostMapping(value = "")
    public R<String> createApprovalTemplate(@Validated @RequestBody ApprovalTemplateReceivedDTO template) {
        return R.data(approvalTemplateBiz.createApprovalTemplate(template).toString());
    }

    /**
     * 校验其中是否有重复数据
     * @param template 参数
     * @return
     */
    @PostMapping("/validation")
    public R<Void> validateTemplateNodeRelation(@Validated @RequestBody ApprovalTemplateReceivedDTO template) {
        ApprovalTemplateDTO approvalTemplateDTO = JsonUtil.string2Obj(template.getProcessData(), ApprovalTemplateDTO.class);
        List<ApprovalTemplateDTO.LinkData> linkDataArray = approvalTemplateDTO.getLinkDataArray();
        List<ApprovalTemplateDTO.NodeData> nodeDataArray = approvalTemplateDTO.getNodeDataArray();
        NodeUtil.validateData(linkDataArray, nodeDataArray);
        return R.ok();
    }

    /**
     * 更新审批模板
     *
     * @param approvalTemplateId
     *            审批模板id
     * @param template
     *            模板
     * @return {@link R}<{@link Void}>
     */
    @Log(title = "编辑审批模板")
    @ApiOperation(value = "编辑审批模板")
    @PutMapping(value = "/{approvalTemplateId}")
    public R<Void> updateApprovalTemplate(@PathVariable Long approvalTemplateId,
        @RequestBody ApprovalTemplateReceivedDTO template) {
        approvalTemplateBiz.updateApprovalTemplate(approvalTemplateId, template);
        return R.ok();
    }

    @ApiOperation(value = "更新模板编码")
    @PutMapping(value = "{approvalTemplateId}/code")
    public R<Void> updateApprovalTemplateCode(@PathVariable Long approvalTemplateId, @Validated @RequestBody ApprovalTemplateCodeDTO templateCode) {
        approvalTemplateBiz.updateApprovalTemplateCode(approvalTemplateId, templateCode);
        return R.ok();
    }

    /**
     * 审批模板详情
     *
     * @param approvalTemplateId
     *            审批模板id
     * @return {@link R}<{@link ApprovalTemplateDetailVO}>
     */
    @ApiOperation(value = "审批模板详情")
    @GetMapping(value = "/{approvalTemplateId}")
    public R<ApprovalTemplateDetailVO> getApprovalTemplate(@PathVariable Long approvalTemplateId) {
        return R.ok(approvalTemplateBiz.getApprovalTemplate(approvalTemplateId));
    }

    /**
     * 分页查询模板详情
     *
     * @param pageNum
     *            页面num
     * @param pageSize
     *            页面大小
     * @param template
     *            模板
     * @return {@link R}<{@link Page}<{@link ApprovalTemplatePo}>>
     */
    @ApiOperation(value = "分页查询模板")
    @GetMapping(value = "/page")
    public R<Page<ApprovalTemplateVo>> pageApprovalTemplate(@RequestParam(defaultValue = "1") Integer pageNum,
        @RequestParam(defaultValue = "20") Integer pageSize, ApprovalTemplateQueryDTO template) {
        return R.ok(approvalTemplateBiz.pageApprovalTemplate(pageNum, pageSize, template));
    }

    @PutMapping("{approvalTemplateId}/status")
    public R<Void> switchStatus(@PathVariable Long approvalTemplateId, Integer status) {
        approvalTemplateBiz.switchStatus(approvalTemplateId, status);
        return R.ok();
    }

    @GetMapping("/count/statistics")
    public R<Integer> statisticsNumber() {
        return R.data(approvalTemplateBiz.statisticsNumber());
    }
}
